package com.nova.bluetooth.helpUtils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.transition.Transition;



public class GlideUtil {
    public static void loadImg(Context context, String url, ImageView imageView) {
        Glide.with(context).load(url).into(imageView);
    }

    public static void loadImg(Context context, String url, ImageView imageView, int res) {
        Glide.with(context).load(url).error(res).into(imageView);
    }

    public static void loadImg(Activity activity, String url, ImageView imageView) {
        Glide.with(activity).load(url).into(imageView);
    }

    public static void loadImg(FragmentActivity activity, String url, ImageView imageView) {
        Glide.with(activity).load(url).into(imageView);
    }

    public static void loadImg(Fragment fragment, String url, ImageView imageView) {
        Glide.with(fragment).load(url).into(imageView);
    }

    public static void loadImg(View view, String url, ImageView imageView) {
        Glide.with(view).load(url).into(imageView);
    }

    public static void loadImg(View view, String url, int width, int height, ImageView imageView) {
        Glide.with(view).load(url)
                .override(width, height)
                .into(imageView);
    }

    public static void loadImg(View view, @RawRes @DrawableRes @Nullable int drawable, ImageView imageView) {
        Glide.with(view).load(drawable)
                .into(imageView);
    }

    public static void loadImg(View view, int url, int width, int height, ImageView imageView) {
        Glide.with(view).load(url)
                .override(width, height)
                .into(imageView);
    }

    public static void loadImg(RequestManager manager, String url, ImageView imageView) {
        manager.load(url).into(imageView);
    }

    public static void loadCircleImg(Context context, String url, ImageView imageView, int res) {
        Glide.with(context)
                .asBitmap()
                .load(url)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .error(res)
                .into(imageView);
    }

    public static void loadRoundedImg(View view, String url, ImageView imageView, int roundingRadius) {
        //Glide设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(roundingRadius);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(20, 20);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        Glide.with(view)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .centerCrop()
                .apply(options)
                .into(imageView);
    }



    public static void loadRoundedImg(View view, String url, ImageView imageView, int roundingRadius, int error) {
        //Glide设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(roundingRadius);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(20, 20);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        Glide.with(view)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .centerCrop()
                .error(error)
                .apply(options)
                .into(imageView);
    }

    public static void loadRoundedImg(Fragment fragment, String url, ImageView imageView, int roundingRadius) {
        //Glide设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(roundingRadius);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(20, 20);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
        Glide.with(fragment)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .centerCrop()
                .apply(options)
                .into(imageView);
    }

    public static void loadWithWatermark(Context context, String imageUrl, ImageView imageView, @DrawableRes int watermark) {
        Glide.with(context)
                .asBitmap()
                .load(imageUrl)
                .into(new BitmapImageViewTarget(imageView) {
                    @Override
                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                        super.onResourceReady(addWatermarkToBitmap(context, resource, watermark), transition);
                    }
                });
    }


    public static Bitmap addWatermarkToBitmap(Context context, Bitmap sourceBitmap, @DrawableRes int watermark) {
        int width = sourceBitmap.getWidth();
        int height = sourceBitmap.getHeight();

        Bitmap resultBitmap = Bitmap.createBitmap(width, height, sourceBitmap.getConfig());
        Canvas canvas = new Canvas(resultBitmap);
        canvas.drawBitmap(sourceBitmap, 0, 0, null);

        Paint paint = new Paint();
        paint.setAntiAlias(true);

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), watermark);
        float wid = (int) AppUtil.dp2px(context, 33f);
        float round = width * 1.0f / height;

        float left = ((int) AppUtil.dp2px(context, 11f)) * round;
        float bottom = height - ((int) AppUtil.dp2px(context, 12f) + wid) * round;
        bitmap = Bitmap.createScaledBitmap(bitmap, (int) (wid * round), (int) (wid * round), false);

        canvas.drawBitmap(bitmap, left, bottom, paint);

        return resultBitmap;
    }


    public static void loadCenterCropRoundedImg(Fragment fragment, String url,
                                                ImageView imageView, int roundingRadius) {
        //Glide设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(roundingRadius);
        Glide.with(fragment)
                .load(url)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .dontAnimate()
                .transform(new CenterCrop(), roundedCorners)
                .into(imageView);
    }


}
